###
# javacompile.rb
#
# Java Compilation mixin for the Metasploit Framework.
# This mixin enables on-the-fly Java code compilation.
#
# by Trancer - mtrancer[AT]gmail.com
# 
# Website: 	http://www.rec-sec.com
# Project page: http://code.google.com/p/metasploit-java-compilation-mixin/
#
###

module Msf
module Exploit::JAVACOMPILE
	
	def initialize(info = {})
		super

		register_options(
			[
				OptString.new('OUTPUTPATH', [ true, 'Working directory location.', File.join(Msf::Config.install_root, 'data', 'exploits', 'java')]),
			], Msf::Exploit::JAVACOMPILE
		)
	end

	def java_compile(filename,data)	
		binfile = ''
		srcfile = filename + ".java"
		outfile = filename + ".class"
		srcpath = File.expand_path(File.join(datastore['OUTPUTPATH'], srcfile))
		outpath = File.expand_path(File.join(datastore['OUTPUTPATH'], outfile))
		tmpfile = File.new(srcpath,"wb")
		tmpfile.write(data)
		tmpfile.close
		print_status("File #{srcfile} created.")
		system("javac #{srcpath}")
		if !File.exist?("#{outpath}")
			print_status("File #{srcfile} did not compile.")
		else
			print_status("File #{outfile} created.")
			jfile = File.open(outpath,"rb")
			binfile = jfile.read
			jfile.close
			return binfile
		end
	end

	def java_clean(filename)	
		srcfile = filename + ".java"
		outfile = filename + ".class"
		srcpath = File.expand_path(File.join(datastore['OUTPUTPATH'], srcfile))
		outpath = File.expand_path(File.join(datastore['OUTPUTPATH'], outfile))
		File.delete(srcpath)
		File.delete(outpath)
		print_status("Working directory cleaned.")
		return
	end	

end
end